package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"time"
)

type JSONTime struct {
	time.Time
}

// ceshi
func (t *JSONTime) MarshalJSON() ([]byte, error) {
	// 这是个奇葩,必须是这个时间点, 据说是go诞生之日, 记忆方法:6-1-2-3-4-5
	return []byte(fmt.Sprintf(`"%s"`, t.Format("2006-01-02 15:04:05"))), nil
}

func (t *JSONTime) UnmarshalJSON(data []byte) error {
	var err error

	t.Time, err = time.Parse(`"2006-01-02 15:04:05"`, string(data))
	if err != nil {
		return err
	}

	return nil
}

type Time struct {
	T JSONTime `json:"t,omitempty"`
}

func main2() {
	// 测试 time 及其接口
	Formatstr := "2006-01-02 15:04:05"
	fmt.Println(time.April)
	fmt.Println(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
	fmt.Println(time.Now().Format(Formatstr))
	t0 := time.Now()

	time.Sleep(100 * time.Millisecond)

	t1 := time.Now()

	fmt.Println(t1.Sub(t0))

	t := &Time{
		T: JSONTime{time.Now()},
	}

	tbytes, _ := json.Marshal(t)
	fmt.Println("json 格式输出时间戳：", string(tbytes))

	t = &Time{}

	_ = json.Unmarshal(tbytes, t)
	fmt.Println(t.T)

	t2 := time.Now().Unix()
	fmt.Println(t2)

	t3 := time.Unix(t2, 0)
	fmt.Println(t3.Format(Formatstr))
	//fmt.Println(t3.AppendFormat(1))

}

func main3() {
	file, err := os.OpenFile("./time.txt", os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Printf("打开文件错误= %v \n", err)
		return
	}
	w := bufio.NewWriter(file)
	defer file.Close()
	for i := 0; i < 10; i++ {
		t1 := time.Now()
		time.Sleep(31 * time.Millisecond)
		t2 := time.Now()
		c := t2.Sub(t1).String()
		fmt.Println("耗时：", t2.Sub(t1).String())
		w.WriteString(c[0:len(c)-2] + ",")
		w.Flush()
	}

}

//

var t *time.Ticker
var Msg []string

func init() {
	t = time.NewTicker(5 * time.Second)
}

// 测试计时器
func main() {

	//客户端不停发送数据
	t := time.Now()
	// tt := time.Now()
	// tt = tt.Add(20 * time.Second)

	i := 0
	for {
		i++
		time.Sleep(100 * time.Millisecond)
		data := "test" + strconv.Itoa(i)
		send(data)

		if time.Since(t) >= 20*time.Second {
			break
		}

		// if time.Now().After(tt) {
		// 	break
		// }

	}

	// fmt.Println("ticker test")
	// //每隔5秒打印一次 消息
	// tiker := time.NewTicker(5 * time.Second)
	// for {
	// 	<-tiker.C
	// 	fmt.Println("ticker:", time.Now().Unix())
	// }
	// // select {}
}

func send(data string) {

	fmt.Println("收到消息", data)
	Msg = append(Msg, data) // 把消息存起来
	//开启计时器

	//这里不能阻塞
	//go func() {
	<-t.C
	fmt.Println("每隔5秒记录一次：", time.Now().Unix(), Msg)
	Msg = Msg[0:0]
	//}()

	// t := false
	//go keepAccount(Msg)
	// if t {
	// 	fmt.Println("记账了：", time.Now().Unix(), Msg)
	// 	// 清空切片
	// }

}

func keepAccount(Msg []string) {
	<-t.C
	fmt.Println("每隔5秒记录一次：", time.Now().Unix(), Msg)
	Msg = Msg[0:0]
}

func timertest() {
	fmt.Println("timer test")
	timer := time.NewTimer(5 * time.Second)
	for {
		<-timer.C
		fmt.Println("timer:", time.Now().Unix())
	}
	timer.Reset(time.Second)
}

func main66() {
	ret := []string{"1121", "sffsf"}
	fmt.Println("ret", ret)
	ret = ret[0:0]
	fmt.Println("ret2", ret)
}

// SliceClear 切片清空
func SliceClear(s *[]interface{}) {
	*s = append([]interface{}{})
}
